define(['jquery', 'frontend/loginpop', 'frontend/xiao_man_yao_task'], function ($, LoginPop, XiaoManYao_Task) {

    var Controller = {
        ai_api: Config.ai_api,
        //所在页面
        page_name: 'classroom',
        //初始化
        init: function () {

            //监听Escape按下
            document.addEventListener('keydown', function (event) {
                if (event.key === "Escape" || event.keyCode === 27) {
                    // if ($(".xiao-man-yao-body").css("display") == "block") {

                    //     //空中教室才可以关闭它
                    //     if (Controller.page_name == "classroom") {
                    //         console.log('打开了小蛮妖，关闭它');
                    //         $(".xiao-man-yao-body").hide();
                    //     }
                    // }
                }
            });

            //小蛮妖的按钮
            $(".xiao-man-yao").click(function () {

                if (LoginPop.logined == 0) {
                    LoginPop.openLoginWindow();
                    return false;
                }
                Controller.openAIWindow();
            })

            //发送消息的按钮
            $(".msg-send-btn").click(function () {

                console.log("LoginPop cross_site_token", LoginPop.cross_site_token);
                if (LoginPop.logined == 0) {
                    LoginPop.openLoginWindow();
                    return false;
                }

                if (XiaoManYao_Task.doing_task) {
                    layer.msg('正在查询中，请等待查询结果', { icon: 6, time: 2000 });
                    return false;
                }

                var message = $(".msg-text").val();
                if (message == "") {
                    layer.msg('发送内容不能为空', { icon: 6, time: 2000 });
                    return false;
                }

                //选中的任务项
                var checked_job = $("input[name='job_name']:checked");

                var job_type = $(checked_job).val();
                console.log("job_type", job_type);

                var role_id = $(checked_job).attr("role_id");
                console.log("role_id", role_id);

                var prompt_id = $(checked_job).attr("prompt_id");
                console.log("prompt_id", prompt_id);

                var pre_text = $(checked_job).attr("pre_text");
                console.log("pre_text", pre_text);

                XiaoManYao_Task.ai_api_error = "";//清空之前的错误
                XiaoManYao_Task.lists = [];//清空之前的结果
                XiaoManYao_Task.ai_tasks = [];//清空之前的任务队列
                $("input[name='ai-model']:checked").each(function () {
                    XiaoManYao_Task.ai_tasks.push({
                        "ai": $(this).attr("ai"),
                        "ai_name": $(this).attr("ai_name"),
                        "job_type": job_type,
                        "message": message,
                        "pre_text": pre_text,
                        "role_id": role_id,
                        "prompt_id": prompt_id,
                    });
                });

                //就算没有选任何AI渠道，设置默认的查询AI渠道
                if (XiaoManYao_Task.ai_tasks.length == 0) {
                    XiaoManYao_Task.ai_tasks.push({
                        "ai": "hunyuan4",
                        "ai_name": "腾讯混元4.0",
                        "job_type": job_type,
                        "message": message,
                        "pre_text": pre_text,
                        "role_id": role_id,
                        "prompt_id": prompt_id,
                    });
                }

                console.log("ai_tasks", XiaoManYao_Task.ai_tasks);

                XiaoManYao_Task.ai_api = Controller.ai_api;
                XiaoManYao_Task.token = LoginPop.cross_site_token;

                //开启任务检测定时器
                XiaoManYao_Task.task_timer = setInterval(function () {
                    XiaoManYao_Task.enterTask();
                }, 1000);

            });
        },
        //打开AI小窗口
        openAIWindow: function () {
            $(".xiao-man-yao-body").css("display") == "block" ? $(".xiao-man-yao-body").css("display", "none") : $(".xiao-man-yao-body").css("display", "block");
        },
    };
    return Controller;
});
